<?php

	require_once 'connection.php';
	
	class Customer{
		static function getInfoProduct($productID){
			$query="SELECT * FROM tbl_product pr, tbl_brand br,tbl_model md,tbl_series sr,tbl_product_detail pr_dt WHERE
			pr.brandID=br.brandID AND pr.modelID=md.modelID AND pr.seriesID=sr.seriesID AND pr_dt.productID=pr.productID AND pr.productID='$productID' GROUP BY pr.brandID,pr.modelID,pr.seriesID";
			$result=mysql_query($query) or die("getInfoProduct: ".mysql_error());
			$row=mysql_fetch_array($result);
			return $row;
		}
		static function orders_history($customerID){
			$query="SELECT * FROM tbl_product_detail pr_dt,tbl_orders ors,tbl_orders_detail dt WHERE
			dt.productID=pr_dt.productID AND dt.ordersID=ors.ordersID AND customerID='$customerID' ORDER BY ors.ordersID DESC LIMIT 10";
			
			$result=mysql_query($query) or die("purchase_history".mysql_error());
				print ('<table id="hor-zebra">
			<thread><tr>
				<th>Tên sản phẩm</th>
				<th>Số lượng</th>
				<th>Đơn giá</th>
				<th>Tổng tiền</th>
				<th>Ngày giao dịch</th>
				<th>Trạng thái</th>
			</tr></thread>
			');
			$value=0;
			while($row=mysql_fetch_array($result)){
			$get=Product::getInfoProduct($row['productID']);
				print ('<tbody>');
				if($value%2==0){echo '<tr class="tr-1">';}
				else {echo '<tr class="tr-2">';}
				print ('
							<td>'.$get['brandName'].' '.$get['modelName'].' '.$get['seriesName'].'</td>
							<td>'.$row['or_quantity'].'</td>
							<td>'.number_format($row['or_price'],3).'</td>
							<td>'.number_format($row['totalPrice'],3).'</td>
							<td>'.gmdate("d/m/Y H:i A", $row['or_dateAdd'] + 7*3600).'</td>
							<td>'.$row['or_status'].'</td>
							</tr>');
			$value+=1;
							}
			print('</tbody></table>');
		}
			/*Customer*/
		static function redirectTo($page){
			$host=$_SERVER['HTTP_HOST'];
			$uri= rtrim(dirname($_SERVER['PHP_SELF']),'/\\');
			header("Location: http://$host/$uri/$page");
			exit();
		}
		//ma hoa matKhau
		static function encryptpassword($password){
			$password=crypt($password,SALT_MD5);
			$password=md5($password);
			return $password;
		}
		
		//check mail
		static function checkEmail($email){
			$email=mysql_real_escape_string(trim($email));
			$query="SELECT *FROM tbl_customer WHERE email='$email'";
			$result=mysql_query($query) or die("checkEmail:".mysql_error());
			$num=mysql_num_rows($result);
			if($num==0)	return true;
			else 		return false;
		}
		//kiểm tra thông tin login
		static function checkCustomerLogin($email,$password){
			$query="SELECT *FROM tbl_customer WHERE email='$email' and password='$password'";
			//echo $query;
			$result=mysql_query($query) or die ("checkUserLogin: ".mysql_error());
			$row=mysql_fetch_array($result);
			return $row;
		}

		//lấy thông tin customer
		static function getCustomerInfo($customerID){
			$query="SELECT * FROM tbl_customer WHERE customerID='$customerID'";
			$result=mysql_query($query) or die("getCustomertInfo".mysql_error());
			$row=mysql_fetch_array($result);
			return $row;
		}
		//them 1 customer moi
		static function addCustomer($f_name,$l_name,$gender,$day,$month,$year,$email,$password,$phone,$address,$city){
			$f_name=mysql_real_escape_string(trim($f_name));
			$l_name=mysql_real_escape_string(trim($l_name));
			$gender=mysql_real_escape_string(trim($gender));
			$email=mysql_real_escape_string(trim($email));
			$password=mysql_real_escape_string(trim($password));
			//$crypt
			$crypt_password=Customer::encryptpassword($password);
			$phone=mysql_real_escape_string(trim($phone));
			$address=mysql_real_escape_string(trim($address));
			$city=mysql_real_escape_string(trim($city));
			$status="active";
			$dateAdd=date("Y-m-d H-i-s");
			$query="INSERT INTO tbl_customer(f_name,l_name,gender,day,month,year,email,password,phone,address,city,status,dateAdd)
			VALUES('$f_name','$l_name','$gender','$day','$month','$year','$email','$crypt_password','$phone','$address','$city','$status','$dateAdd')";
			$result=mysql_query($query) or die("addNewCustomer".mysql_error());
			return $result;
		}
		//cap nhat 1 customer moi
		static function updateCustomer($customerID,$f_name,$l_name,$gender,$day,$month,$year,$email,$phone,$address,$city){
			$f_name=mysql_real_escape_string(trim($f_name));
			$l_name=mysql_real_escape_string(trim($l_name));
			$gender=mysql_real_escape_string(trim($gender));
			$email=mysql_real_escape_string(trim($email));
			$phone=mysql_real_escape_string(trim($phone));
			$address=mysql_real_escape_string(trim($address));
			$city=mysql_real_escape_string(trim($city));
			$status="active";
			$dateAdd=date("Y-m-d H-i-s");
			$query="UPDATE tbl_customer SET f_name='$f_name',l_name='$l_name',gender='$gender',day='$day',
			month='$month',year='$year',email='$email',phone='$phone',address='$address',city='$city',
			status='$status',dateAdd='$dateAdd' WHERE customerID='$customerID'";
			//echo $query;
			$result=mysql_query($query) or die("updateCustomer".mysql_error());
			return $result;
		}
		//cap nhat diA  chi giao hang
		static function updateCustomer_add($customerID,$f_name,$l_name,$email,$phone,$address,$city){
			$f_name=mysql_real_escape_string(trim($f_name));
			$l_name=mysql_real_escape_string(trim($l_name));
			$email=mysql_real_escape_string(trim($email));
			$phone=mysql_real_escape_string(trim($phone));
			$address=mysql_real_escape_string(trim($address));
			$city=mysql_real_escape_string(trim($city));
			$query="UPDATE tbl_customer SET f_name='$f_name',l_name='$l_name',email='$email',phone='$phone',address='$address',city='$city'
			 WHERE customerID='$customerID'";
			echo $query;
			$result=mysql_query($query) or die("updateCustomer_add".mysql_error());
			return $result;
		}
		//thay mat khau
		static function resetPassword($customerID,$passwordNew){
			$passwordNew=mysql_real_escape_string(trim($passwordNew));
			$query="UPDATE tbl_customer SET password='$passwordNew'WHERE customerID='$customerID'";
			//echo $query;
			$result=mysql_query($query) or die("updateCustomer".mysql_error());
			return $result;
			
		}
		//Yeu cau nap tien vao tai khoan
		static function addRequest($customerID,$money){
		$customerID=mysql_real_escape_string(trim($customerID));
		$money=mysql_real_escape_string(trim($money));
		$status="pendding";
		$time=time();
		$query="INSERT INTO tbl_request(customerID,money,status,time)VALUES('$customerID','$money','$status','$time')";
		$result=mysql_query($query) or die("request".mysql_error());
		return $result;
		}
		//thanh toan xu
		static function payment($customerID,$money){
		$query="UPDATE tbl_customer SET money='$money' WHERE customerID='$customerID'";
		//echo $query;
		$result=mysql_query($query) or die("paymentr".mysql_error());
		return $result;
		}
		//thanh toan truc tiep
		static function payment_tt($customerID,$money){
		$query="UPDATE tbl_customer SET money='$money' WHERE customerID='$customerID'";
		//echo $query;
		$result=mysql_query($query) or die("paymentr".mysql_error());
		return $result;
		}
	}
?>